package com.edais.actions;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.edais.domain.UserRank;
import com.edais.service.UserRankService;
import com.edais.util.CommonUtil;
import com.edais.util.Consts;
import com.edais.util.Message;

@Controller
@RequestMapping("/userRank")
public class UserRankAction implements Serializable {

	private static final long serialVersionUID = 1L;

	private static final Log logger = LogFactory.getLog(UserRankAction.class);

	@Autowired
	private UserRankService userRankService;

	@RequestMapping("/queryUserRankList")
	public void queryUserRankList(HttpServletRequest request, HttpServletResponse response) {

		// 初始化
		String loggerInfo = "queryUserRankList";
		int totlePages = 0;
		int current = 1;
		int totalNum = 0;
		int pageSize = 100;
		String result = null;
		List<UserRank> userRankList = new ArrayList<UserRank>();
		Map<String, Object> mapResult = new HashMap<String, Object>();// 返回结果集
		mapResult.put("list", userRankList);
		mapResult.put("listSize", 0);
		mapResult.put("totlePages", totlePages);
		mapResult.put("current", current);
		mapResult.put("totalNum", totalNum);

		logger.info(loggerInfo + "：开始");

		try {
			Map<String, String> paramsMap = CommonUtil.decryptParamters(request);// 解密请求参数
			// 分页参数
			String pageSizeStr = paramsMap.get(Consts.Page.PAGE_SIZE_NAME);// 每页行数
			String currentStr = paramsMap.get(Consts.Page.PAGE_CURRENT_NAME);// 当前页

			if (StringUtils.isNotBlank(currentStr)) {
				pageSize = Integer.valueOf(pageSizeStr);
			}

			if (StringUtils.isNotBlank(currentStr)) {
				current = Integer.valueOf(currentStr);
			}

			logger.info(loggerInfo + " param {pageSize:" + pageSize + ",current:" + current + "}");

			Map<String, Object> queryParamMap = new HashMap<String, Object>();// 查询参数map
			queryParamMap.put("rowBegin", (current - 1) * pageSize);
			queryParamMap.put("rowEnd", pageSize);
			logger.info(loggerInfo + " param queryParamMap:" + queryParamMap.toString());
			// 排行榜总条数
			totalNum = userRankService.queryUserRankListCount();
			if (totalNum < 1) {
				result = CommonUtil.setResultStringCn(mapResult, Consts.SUCCESS_CODE, "", "");
				logger.info(loggerInfo + " 参数为空返回" + result);
				CommonUtil.responseJson(result, response);
				logger.info(loggerInfo + " 结束");
				return;
			}

			if (totalNum % Integer.valueOf(pageSize) == 0) {
				totlePages = totalNum / Integer.valueOf(pageSize);
			} else {
				totlePages = totalNum / Integer.valueOf(pageSize) + 1;
			}

			userRankList = userRankService.queryUserRankList(loggerInfo,queryParamMap);

			// 返回结果集
			mapResult.put("list", userRankList);
			mapResult.put("listSize", userRankList.size());
			mapResult.put("totlePages", totlePages);
			mapResult.put("current", current);
			mapResult.put("totalNum", totalNum);
			result = CommonUtil.setResultStringCn(mapResult, Consts.SUCCESS_CODE, Consts.SUCCESS_DESCRIBE, "");

			CommonUtil.responseJson(result, response);

		} catch (Exception e) {
			logger.info("/userRank/queryUserRankList出现异常-----" + e.getMessage());
			result = CommonUtil.setResultStringCn(new HashMap<String, Object>(), Consts.ERROR_CODE,
					Consts.ErrorMsg.MSG00001, Message.CnDescriptionExpression.BUSINESSE_ERROR);
			CommonUtil.responseJson(result, response);
			return;
		}
	}
}
